<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card class="box-card">
        <div slot="header" class="clearfix">
          <el-button type="primary" @click="isShow=true">添加页面</el-button>
          <div style="float:right;width:50%">
            <el-input v-model="con" placeholder="请输入内容">
              <template slot="append">
                <el-button slot="append" icon="el-icon-search" @click="searchFn" />
              </template>
            </el-input>
          </div>
        </div>
        <el-divider />
        <!-- -------------------- -->
        <el-table border :data="tableData" style="width: 100%">
          <el-table-column align="center" prop="title" label="标题" span="8" />
          <el-table-column v-slot="{row}" align="center" span="8" label="内容">
            <div v-html="row.content" />
          </el-table-column>
          <el-table-column v-slot="{row}" align="center" label="操作">
            <el-button type="primary" @click="editFn(row.id)">编辑</el-button>
            <el-button type="danger" @click="delFn(row.id)">删除</el-button>
          </el-table-column>
        </el-table>
        <el-pagination
          style="text-align:center;padding-top:20px"
          background
          layout="prev, pager, next,total"
          :total="total"
          :page-size="pageConfig.size"
          @current-change="currentChange"
        />

      </el-card>
    </div>

    <DiaLog ref="data" :is-show-dia-log.sync="isShow" @addPage="addPage" />

  </div>
</template>

<script>
import { getPagesList, getPagesListCount, delPage } from '@/api/pageManagement'
import DiaLog from './components/DiaLog.vue'

export default {
  components: {
    DiaLog

  },
  data() {
    return {

      total: 100,
      isShow: false,
      newPage: 1,
      con: '',

      pageConfig: {
        size: 5,
        fir: 0

      },

      tableData: []
    }
  },
  created() {
    this.getList(this.pageConfig.size, this.pageConfig.fir)
    this.getTotal()
  },
  methods: {
    async currentChange(val) {
      this.newPage = val
      this.getList(this.pageConfig.size, (val - 1) * this.pageConfig.size)
    },
    async getList(size, sta, con) {
      this.tableData = await getPagesList({ _limit: size, _start: sta, title_contains: con })
    },
    async getTotal(value) {
      this.total = await getPagesListCount({ title_contains: value })
    },
    // 删除页面
    async delFn(id) {
      await this.$confirm('是否确认删除')

      await delPage(id)
      await this.getTotal()
      await this.getList(this.pageConfig.size, (this.newPage - 1) * this.pageConfig.size)
      this.$message.success('删除成功')
    },
    async addPage() {
      await this.getTotal()
      await this.getList(this.pageConfig.size, (this.newPage - 1) * this.pageConfig.size)
    },
    async searchFn() {
      await this.getTotal(this.con)
      this.getList(this.pageConfig.size, this.pageConfig.fir, this.con)
    },
    editFn(v) {
      this.isShow = true
      this.$refs.data.getPageData(v)
    }

  }

}
</script>

<style lang="scss" scoped>

</style>
